package data

import (
	"crypto/rand"
	"crypto/sha1"
	"database/sql"
	"fmt"

	"log"

	_ "github.com/lib/pq"
)

var Db *sql.DB

// 初始化数据库
func init() {
	var err error
	Db, err = sql.Open("postgres", "user=postgres password=postgres dbname=gallery sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
}

// 创建一个随机UUID
func createUUID() (uuid string) {
	u := new([16]byte)
	_, err := rand.Read(u[:])

	if err != nil {
		log.Fatalln("Cannot generate UUID", err)
	}

	u[8] = (u[8] | 0x40) & 0x7f
	u[6] = (u[6] & 0xf) | (0x4 << 4)
	uuid = fmt.Sprintf("%x-%x-%x-%x-%x",
		u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
	return
}

func Encrypt(plaintext string) (cryptext string) {
	cryptext = fmt.Sprintf("%x", sha1.Sum([]byte(plaintext)))
	return
}
